<template>
  <div class="detail-container">
    <div class="app-container">
      员工详情页面 -- {{ $route.params.id }}
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="登录账户设置" name="login">
            <el-form :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="userInfo.newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="updateUserInfo">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情" name="userInfo">

            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息" name="jobInfo">

            <JobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetUserDetailById } from '@/api/user'
import { reqSaveUserDetailById } from '@/api/employees'
import UserInfo from './UserInfo.vue'
import JobInfo from './JobInfo.vue'
export default {
  name: 'DetailPage',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      activeName: 'login',
      // 登录账户信息关联的表单
      userInfo: {
        username: '',
        newPassword: ''
      },
      // 规则
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    // 根据id获取个人信息
    async  getUserInfo() {
      const { data } = await reqGetUserDetailById(this.$route.params.id)
      this.userInfo = data
    },
    // 重置密码
    async updateUserInfo() {
      await reqSaveUserDetailById({ ...this.userInfo, password: this.userInfo.newPassword })
      this.userInfo.newPassword = ''
    }
  }
}
</script>

<style>

</style>
